<?php

namespace app\admin\controller;

use app\admin\model\ActionLog;
use app\admin\service\ActionLogService;
use app\admin\validate\ActionLogValidate;
use common\apidoc\Definitions;
use common\enum\ResultCode;
use hg\apidoc\annotation as Apidoc;
use support\Request;
use support\Response;
use support\Container;
use Tinywan\ExceptionHandler\Exception\BadRequestHttpException;

#[Apidoc\Title("行为日志管理")]
#[Apidoc\Sort(998)]
class ActionLogController extends BaseController
{
    private ActionLogService $service;
    protected ActionLogValidate $validate;

    public function __construct()
    {
        $this->service = Container::get(ActionLogService::class);
        $this->validate = Container::get(ActionLogValidate::class);
    }

    #[Apidoc\Title("行为日志列表")]
    #[Apidoc\Url("admin/actionlog/index")]
    #[Apidoc\Method("GET")]
    #[Apidoc\Query(ref: [Definitions::class, "pagingQueryParam"])]
    #[Apidoc\Query("admin_name", type: "string", desc: "管理员名称")]
    #[Apidoc\Query("status", type: "int", desc: "状态:0=失败,1=成功")]
    #[Apidoc\Query("ip", type: "string", desc: "IP地址")]
    #[Apidoc\Query("start_time", type: "string", desc: "开始时间")]
    #[Apidoc\Query("end_time", type: "string", desc: "结束时间")]
    #[Apidoc\Returned(ref: [Definitions::class, "pagingReturnParam"])]
    #[Apidoc\Returned("list", type: "array", ref: ActionLog::class, desc: "行为日志列表")]
    public function index(Request $request): Response
    {
        $params = [
            'page' => $request->get('page', 1),
            'limit' => $request->get('limit', 10),
            'admin_name' => $request->get('admin_name', ''),
            'status' => $request->get('status', ''),
            'ip' => $request->get('ip', ''),
            'start_time' => $request->get('start_time', ''),
            'end_time' => $request->get('end_time', ''),
        ];
        if (!$this->validate->check($params, 'index')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->getList($params);
        return $this->success($result);
    }

    #[Apidoc\Title("行为日志详情")]
    #[Apidoc\Url("admin/actionlog/info")]
    #[Apidoc\Method("GET")]
    #[Apidoc\Query("id", type: "int", require: true, desc: "日志ID")]
    #[Apidoc\Returned(type: "object", ref: ActionLog::class, desc: "行为日志详情")]
    public function info(Request $request): Response
    {
        $id = $request->get('id');
        if (!$this->validate->check(['id' => $id], 'info')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $result = $this->service->getInfo($id);
        return $this->success($result);
    }

    #[Apidoc\Title("删除行为日志")]
    #[Apidoc\Url("admin/actionlog/delete")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("id", type: "int", require: true, desc: "日志ID")]
    public function delete(Request $request): Response
    {
        $id = $request->post('id');
        if (!$this->validate->check(['id' => $id], 'delete')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $this->service->delete($id);
        return $this->success(null, ResultCode::DELETE_SUCCESS->getDescription());
    }

    #[Apidoc\Title("批量删除行为日志")]
    #[Apidoc\Url("admin/actionlog/batchDelete")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Param("ids", type: "array", require: true, desc: "日志ID数组")]
    public function batchDelete(Request $request): Response
    {
        $ids = $request->post('ids');
        if (!$this->validate->check(['ids' => $ids], 'batchDelete')) {
            throw new BadRequestHttpException($this->validate->getError());
        }
        $this->service->batchDelete($ids);
        return $this->success(null, ResultCode::DELETE_SUCCESS->getDescription());
    }
}